package com.cy.oa.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cy.oa.entity.LeaveBean;
import com.cy.oa.service.LeaveService;
import com.cy.system.config.DataSourceContextHolder;

@Controller
@RequestMapping("/leave")
public class LeaveController {
	@Resource
	LeaveService leaveService;
	@Resource
	TaskService taskService;
	@Resource
	RuntimeService runtimeService;
	@Resource
	RepositoryService repositoryService;
	
	@RequestMapping("/toReLeaveEditPage")
	@ResponseBody
	public ModelAndView toReLeaveEditPage() {
		return new ModelAndView("oa/leave/leaveEditPage");
	}
	
	@RequestMapping("/leaveHandle")
	public String applyLeave(String instanceId,Model model) {
		List<String> btnList = new ArrayList<String>();
		//1、通过实例Id,获取LeaveBean、Task、流程实例
		LeaveBean leave = leaveService.queryLeaveByInstanceId(instanceId);
		Task task = taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
		ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(instanceId).singleResult();
		//2、通过task，获取流程定义Id
		String processDefinitionId= task.getProcessDefinitionId();
		//3、获取流程定义实体，进而获取流程活动实体
		ProcessDefinitionEntity pdfEntity= (ProcessDefinitionEntity) repositoryService.getProcessDefinition(processDefinitionId);
		ActivityImpl activityImpl = pdfEntity.findActivity(processInstance.getActivityId());
		//4、获取当前任务，下连线
		List<PvmTransition> pvmTransitionList = activityImpl.getOutgoingTransitions();
		for(PvmTransition pvm : pvmTransitionList) {
			String outcome = (String) pvm.getProperty("name");
			btnList.add(outcome);
		}
		List<Comment> commentList = taskService.getProcessInstanceComments(instanceId);
		System.out.println(leave);
		System.out.println("task.getId():"+task.getId());
		model.addAttribute("commentList",commentList);
		System.out.println("commentList:"+commentList);
		model.addAttribute("btnList",btnList);
		System.out.println(leave);
		model.addAttribute("leave", leave);
		model.addAttribute("taskId", task.getId());
		return "oa/leave/leaveHandle";
	}
	
	@RequestMapping("/completeTask")
	@ResponseBody
	public Map<String,String> completeTask(String taskId,String leaveId,String pOut,String commentMsg,Model model) {
		/*System.out.println("outcome:"+pOut);
		System.out.println("taskId:"+taskId);*/
		HashMap<String,String> resMap = new HashMap<String,String>();
		DataSourceContextHolder.setDbType("activitiDataSource");
		boolean res = leaveService.completeLeave(taskId, pOut, commentMsg,leaveId);
		DataSourceContextHolder.clearDbType();
		if(res) {
			resMap.put("resMsg","处理成功！");
		}
		return resMap;
	} 
	
	@RequestMapping("/historyIndex")
	public String historyIndex() {
		return "oa/leave/historyIndex";
	}
	
	@RequestMapping("/historyLeaveList")
	public String historyLeaveList() {
		
		return "oa/leave/leaveHistoryListPage";
	}
	
	@RequestMapping("/leaveListRe")
	@ResponseBody
	
	public List<LeaveBean> leaveListRe() {
		List<LeaveBean> resList = leaveService.queryAllLeave();
		
		return resList;
	}
	
}
